﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.Profile;
using System.Web.UI.WebControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Data.SqlClient;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Configuration;
using Huayou.Utils;
using HySoft.Cash;

namespace PayRoller.Member
{
    public class Index : MemberParent, IRequiresSessionState
    {
        // Fields
        public double fAvaBalance;
        public double fFeeBalance;
        public double fCreditLimit;
        public double fCreditBalance;
        public string Guid = "";
        public string InvoiceDate = "";
        public string InvoiceSid = "";
        protected Label lbTip;
        public string OrgName = "";
        public string sActiveCount = "";
        public string sActiveTerminated = "";
        //public string sAvaBalance = "";
        public string sCreditLimit = "";

        // Methods
        //public string GetAvaBalance(int Sid)
        //{
        //    TsysoptionBN nbn = new TsysoptionBN(this.Page);
        //    TsysoptionDT ndt = nbn.Get(Sid);
        //    nbn.Dispose();
        //    nbn = null;
        //    if (ndt != null)
        //    {
        //        return ndt.OptionValue;
        //    }
        //    return "";
        //}

        //public double GetClosingBalance(int OrgSid)
        //{
        //    return TorginfoBN.GetClosingBalance(OrgSid);
        //}

        public void GetLatestInvoice(int OrgSid)
        {
            TinvoicelistDT invoiceDT = TorginfoBN.GetLatestInvoice(OrgSid);
            if (invoiceDT != null)
            {
                this.InvoiceSid = invoiceDT.Sid.ToString();
                this.InvoiceDate = DateUtils.DateFormatWithCulture( invoiceDT.InvoiceDate,AppSettings.CurrentCulture);
                this.Guid = invoiceDT.Guid.ToString();
            }
        }

        public void GetOrgInfo(int OrgSid)
        {
            TorginfoBN obn = new TorginfoBN(this.Page);
            TorginfoDT odt = obn.Get(OrgSid);
            this.OrgName = odt.OrgName;
            this.sCreditLimit = odt.CreditLimit.ToString();
            this.fCreditLimit = Convert.ToDouble(odt.CreditLimit);
            this.fFeeBalance = Convert.ToDouble(odt.FeeBalance);
            this.fCreditBalance = Convert.ToDouble(odt.CreditBalance);
            obn.Dispose();
            obn = null;
        }

        protected override void OnInit(EventArgs e)
        {
            base.CheckLogin();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int orgSid = Convert.ToInt32(base.OrgSid);
                //this.sAvaBalance = this.GetAvaBalance(1);
                this.GetOrgInfo(orgSid);
                this.sActiveCount = this.ReturnCount(orgSid, 1).ToString();
                this.sActiveTerminated = this.ReturnCount(orgSid, 0).ToString();
                this.GetLatestInvoice(orgSid);
                //this.fClosingBalance = this.GetClosingBalance(orgSid);
                this.fAvaBalance = this.fCreditLimit - this.fCreditBalance;
            }
            catch (Exception exception)
            {
                this.lbTip.Text = exception.Message;
            }
        }

        public int ReturnCount(int OrgSid, int IsValid)
        {
            TempinfoBN obn = new TempinfoBN(this.Page);
            DataTable table = new DataTable();
            obn.QueryOrgSid(OrgSid);
            obn.QueryIsValid(IsValid);
            return obn.GetList().Rows.Count;
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
